package com.abc.core.util;

import com.abc.core.instance.bo.Cell;
import com.abc.core.instance.bo.Inst;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

public class InstSerializer extends JsonSerializer<Inst> {

    @Override
    public void serialize(Inst inst, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("dnaCode", inst.getDnaCode());
        gen.writeStringField("dnaName", inst.getDnaName());
        gen.writeStringField("instType", inst.getInstType());
        gen.writeNumberField("total", inst.getTotal());

        if (inst.getParentCell() != null) {
            gen.writeNumberField("parentId", inst.getParentCell().getId());
        } else {
            gen.writeNumberField("parentId", 0);
        }

        gen.writeArrayFieldStart("cells");
        for (Cell cell : inst.getCells()) {
            gen.writeObject(cell);
        }
        gen.writeEndArray();

        gen.writeEndObject();
    }

}
